package com.shootforthestars.objects;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;

import com.shootforthestars.components.CollisionComponent;
import com.shootforthestars.core.Actor;
import com.shootforthestars.core.Collidable;

public abstract class Obstacle implements Actor, Collidable {
	private Image image;
	private Vector2f velocity;
	private CollisionComponent physics;

	public Obstacle() {
		super();
		velocity = new Vector2f();
		physics = new CollisionComponent();
	}

	@Override
	public final void init() throws SlickException {
		physics.setShape(new Rectangle(-image.getWidth() / 2, -image.getHeight() / 2,
				image.getWidth(), image.getHeight()));
	}

	@Override
	public final void update(Input input, int delta) throws SlickException {
		physics.moveBy(velocity.x, -velocity.y);
	}

	@Override
	public final void render(Graphics graphics) throws SlickException {
		// Save graphics state
		graphics.pushTransform();

		// Translate to center of
		graphics.translate(-physics.getWidth() / 2, -physics.getHeight() / 2);

		// Draw the image
		graphics.drawImage(image, physics.getX(), physics.getY());
		
		// Restore previous graphics state
		graphics.popTransform();
		
		// DEBUG
//		physics.debug(graphics);
	}

	public void setImage(Image image) {
		this.image = image;
//		physics.setShape(new Rectangle(-image.getWidth() / 2, -image.getHeight() / 2,
//				image.getWidth(), image.getHeight()));
	}

	public void setVelocity(float x, float y) {
		velocity.x = x;
		velocity.y = y;
	}

	public CollisionComponent getCollisionComponent() {
		return physics;
	}

	/**
	 * Callback when obstacle is hit by the rocket.
	 * 
	 * @param rocket
	 */
	public abstract void onHitRocket(Rocket rocket);

	/**
	 * @return if the obstacle is still alive
	 */
	public boolean isAlive() {
		return false;
	}
}
